package org.dandsoft.blackbox.laboratory.tests.streamer;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Hashtable;

/**
 * @author Vilyuzhanin Denis (Fin)
 */
public class WordsCounter {

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "windows-1251"));
        Map<String, Integer> words = new Hashtable<String, Integer>();
        String str;
        int maxWordLength = 0;
        while ((str = reader.readLine()) != null) {
            if (maxWordLength < str.length()) {
                maxWordLength = str.length();
            }
            for (int i = 1; i < str.length(); i++) {
                for (String s : parse(str, i)) {
                    if (words.containsKey(s)) {
                        int counter = words.get(s) + 1;
                        words.put(s, counter);
                    } else {
                        words.put(s, 1);
                    }
                }
            }
        }

        System.out.println("Size: " + words.size());
        System.out.println("Max length: " + maxWordLength);
        int[] sizes = new int[maxWordLength + 1];
        for (Map.Entry<String, Integer> entry : words.entrySet()) {
            int index = entry.getKey().length();
            sizes[index]++;
        }

        for (int i = 0; i < sizes.length; i++) {
            System.out.println("[" + i + "] " + sizes[i]);
        }

    }


    private static List<String> parse(String str, int length) {
        List<String> words = new ArrayList<String>();
        for (int i = 0; i < str.length() - (length - 1); i++) {
            words.add(str.substring(i, i + length));
        }
        return words;
    }

}
